package com.cardbaobao.cardbabyclient.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DrawFilter;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.view.View;

/**
 * 钟摆效果View
 * Created by zad
 * 2017/1/23 10:54
 */

public class PendulumView extends View {
    /**
     * 钟摆半径,X轴最大可移动距离
     */
    private int mXRadius,mYRadius,mXMaxDistance;
    /**
     * 圆点半径
     */
    private int mCircleRadius;
    /**
     * X轴默认偏移量
     */
    private static final int X_OFFSET = 3;
    /**
     * 默认刷新速度10毫秒
     */
    private static final int DEFAULT_DELAYED = 10;
    /**
     * X轴左移动点
     */
    private int mXLeftPostion;
    /**
     * 左边是否向最大值移动
     */
    private boolean isXLeftToMax = true;
    /**
     * 右边是否向最大值移动
     */
    private boolean isXRightToMax = true;
    /**
     * X轴右移动点
     */
    private int mXRightPostion;
    /**
     * 画笔
     */
    private Paint mPaintCenter,mPaintLeft,mPaintRight;
    /**
     * 画布抗锯齿
     */
    private DrawFilter mDrawFilter;
    /**
     * 红，黄，绿
     */
    private int[] mColors = new int[3];
    /**
     * 循环周期数
     */
    private int mCycleCount = 0;
    /**
     * 是否循环动画
     */
    private boolean isCircleAnim = false;

    public PendulumView(Context context) {
        this(context,null);
    }

    public PendulumView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PendulumView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

        mColors[0] = Color.parseColor("#FF5228");
        mColors[1] = Color.parseColor("#FFD700");
        mColors[2] = Color.parseColor("#BBE142");

        mPaintCenter = new Paint();
        mPaintCenter.setAntiAlias(true);//抗锯齿
        mPaintCenter.setStyle(Paint.Style.FILL);
        mPaintCenter.setColor(mColors[0]);

        mPaintLeft = new Paint();
        mPaintLeft.setAntiAlias(true);//抗锯齿
        mPaintLeft.setStyle(Paint.Style.FILL);
        mPaintLeft.setColor(mColors[1]);

        mPaintRight = new Paint();
        mPaintRight.setAntiAlias(true);//抗锯齿
        mPaintRight.setStyle(Paint.Style.FILL);
        mPaintRight.setColor(mColors[2]);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setDrawFilter(mDrawFilter);//去除绘制锯齿
        canvas.translate(mXRadius,mYRadius);
        //绘制左圆
        drawLeftCircle(canvas);
        //绘制右圆
        drawRightCircle(canvas);
        if(isCircleAnim){
            postInvalidateDelayed(DEFAULT_DELAYED);
        }
    }

    /**
     *  绘制左移动圆点
     * @param canvas
     */
    private void drawLeftCircle(Canvas canvas){
        if(isXLeftToMax){
            //向左移动过程中
            if(mXLeftPostion >= - mXMaxDistance){
                mXLeftPostion -= X_OFFSET;
            }else{
                //返回中心点
                mXLeftPostion = - mXMaxDistance;
                isXLeftToMax = false;
            }
        }else{
            //向右移动过程中
            if(mXLeftPostion <= 0){
                mXLeftPostion +=  X_OFFSET;
            }else{
                mXLeftPostion = 0;
                isXLeftToMax = true;
                if(++mCycleCount > mColors.length - 1){
                    mCycleCount = 0;
                }
            }
        }
        mPaintLeft.setColor(mColors[(mCycleCount + 2) % mColors.length]);
        canvas.drawCircle(mXLeftPostion,0,mCircleRadius,mPaintLeft);
    }

    /**
     *  绘制右移动圆点
     * @param canvas
     */
    private void drawRightCircle(Canvas canvas){
        if(isXRightToMax){
            //向右移动过程中
            if(mXRightPostion <= mXMaxDistance){
                mXRightPostion += X_OFFSET;
            }else{
                mXRightPostion = mXMaxDistance;
                isXRightToMax = false;
            }
        }else{
            //向左移动过程中
            if(mXRightPostion >= 0){
                mXRightPostion -= X_OFFSET;
            }else{
                mXRightPostion = 0;
                isXRightToMax = true;
            }
        }
        //绘制中心圆
        mPaintCenter.setColor(mColors[mCycleCount]);
        canvas.drawCircle(0,0,mCircleRadius,mPaintCenter);
        mPaintRight.setColor(mColors[(mCycleCount + 1) % mColors.length]);
        canvas.drawCircle(mXRightPostion,0,mCircleRadius,mPaintRight);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mXRadius = w / 2;
        mYRadius = h / 2;
        mCircleRadius = mYRadius / 5;
        mXMaxDistance = mXRadius - mCircleRadius * 3 - X_OFFSET;
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == GONE || visibility == INVISIBLE) {
            stopAnimation();
        } else {
            startAnimation();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startAnimation();
    }

    @Override
    protected void onDetachedFromWindow() {
        stopAnimation();
        super.onDetachedFromWindow();
    }

    /**
     * 开始动画
     */
    private void startAnimation() {
        if(!isCircleAnim){
            isCircleAnim = true;
            invalidate();
        }
    }
    /**
     * 停止动画
     */
    private void stopAnimation() {
        isCircleAnim = false;
    }

}
